import os
from PyQt5.QtWidgets import *
from CallfirstMainWin import MyMainWindow as firstMainWin
import sys
from PoetrysToFile import PoetrysToFile
from QuestionStore import QuestionStore
from PoetryExcel import PoetryReadExcel
from Base import Base


class UiPortal(QWidget, Base):
    def __init__(self, name='宝宝的诗词大会'):
        super(UiPortal, self).__init__()
        # 加载基础
        self.PoetryStore = QuestionStore.instance()

        # otherUI
        self.firstMainWin = firstMainWin()
        # PortalUI
        self.setWindowTitle(name)
        self.cwd = os.getcwd()  # 获取当前程序文件位置
        self.resize(300, 200)  # 设置窗体大小
        # btn 诗词题目
        self.btn_poetry_question = QPushButton(self)
        self.btn_poetry_question.setObjectName("btn_poetry_question")
        self.btn_poetry_question.setText("诗词大会")

        # btn 诗词列表
        self.btn_questions_in_word = QPushButton(self)
        self.btn_questions_in_word.setObjectName("btn_questions_in_word")
        self.btn_questions_in_word.setText("诗词试卷")

        # btn 生成诗词列表文档
        self.btn_poetrys_in_word = QPushButton(self)
        self.btn_poetrys_in_word.setObjectName("btn_poetrys_in_word")
        self.btn_poetrys_in_word.setText("诗词列表")

        # btn 软件说明
        self.btn_software_readme = QPushButton(self)
        self.btn_software_readme.setObjectName("btn_questions_in_excel")
        self.btn_software_readme.setText("生成题库")

        # 设置布局
        layout = QVBoxLayout()
        layout.addWidget(self.btn_poetry_question)
        layout.addWidget(self.btn_questions_in_word)
        layout.addWidget(self.btn_poetrys_in_word)
        layout.addWidget(self.btn_software_readme)
        self.setLayout(layout)

        # 设置信号
        self.btn_poetry_question.clicked.connect(self.slot_btn_poetry_question)
        self.btn_questions_in_word.clicked.connect(self.slot_btn_questions_in_word)
        self.btn_poetrys_in_word.clicked.connect(self.slot_btn_poetrys_in_word)
        self.btn_software_readme.clicked.connect(self.slot_btn_questions_in_excel)

        # 出现加载错误弹出提示信息
        if len(self.PoetryStore.error_msg) > 2:
            QMessageBox.about(self, "加载信息", self.PoetryStore.error_msg)

    # 主界面
    def slot_btn_poetry_question(self):
        self.firstMainWin.show()

    # 生成诗词试卷到D盘
    def slot_btn_questions_in_word(self):
        poetrys_to_file = PoetrysToFile()
        msg = "生成以下试卷到D盘：\n"
        for i in range(0, self.cfg.getint('to_word', 'files_count')):
            filename = poetrys_to_file.question_to_word()
            msg = msg + "\n" + filename
        QMessageBox.about(self, "消息", msg)

    # 将当前诗词列表生成到word文档
    def slot_btn_poetrys_in_word(self):
        poetrys_to_file = PoetrysToFile()
        msg = "生成以下试卷到D盘：\n"
        filename = poetrys_to_file.poetrys_to_word()
        msg = msg + "\n【" + filename + "】"
        QMessageBox.about(self, "消息", msg)

    # 把所有问题输出到Excel表格中
    def slot_btn_questions_in_excel(self):
        # 打印问题
        questions = self.PoetryStore.get_all_poetrys_all_questions()
        # '名称,类型,问题,答案,解释'
        pe = PoetryReadExcel.instance()
        filename = pe.write_by_dict_list(questions)
        # 结束
        msg = "当前共【" + str(len(questions) - 1) + "】个问题\n生成以下文件到D盘：\n" + "【" + filename + "】"
        QMessageBox.about(self, "消息", msg)

    def slot_btn_save_file(self):
        file_name_choose, file_type = QFileDialog.getSaveFileName(self, "文件保存", self.cwd,
                                                                  "All Files (*);;Text Files (*.txt)")

        if file_name_choose == "":
            print("\n取消选择")
            return
        print("\n你选择要保存的文件为:")
        print(file_name_choose)
        print("文件筛选器类型: ", file_type)
        return file_name_choose


if __name__ == "__main__":
    app = QApplication(sys.argv)
    po = UiPortal()
    po.show()
    sys.exit(app.exec_())
